<?php
namespace app\admin\controller;

use think\App;
use think\Request;
use app\admin\traits\ValidateTrait;
use think\exception\ValidateException;

class OrderPartDeliveryApply extends Base
{
    use ValidateTrait;

    protected $request;
    protected $service;
    protected $operator_id;
    protected $validationScenes = [
        'index' => [
            'order_id' => ['require|integer', '订单ID'],
        ],
        'cancel' => [
            'id' => ['require|integer', 'ID'],
        ],
        'create' => [
            'order_id' => ['require', '订单ID'],
            'items' => ['require|array', '申请项列表'],
            'items.*.order_item_id' => ['require|integer', '订单项ID'],
            'items.*.apply_quantity' => ['require|integer|gt:0', '申请数量'],
        ],
        'approve' => [
            'id' => ['require|integer', 'ID'],
            'part_delivery_amount' => ['require|float|gt:0', 'ID'],
            'status' => ['require|integer|in:1,2', '状态'],
        ],
    ];

    public function __construct(Request $request)
    {

        $this->request = $request;
        $this->service = new \app\admin\service\OrderPartDeliveryApply();
        $this->operator_id = $request->operator_id;
    }

    /**
     * @describe 获取部分发货申请列表
     * @return \think\Response
     */
    public function index()
    {
        $params = $this->request->param();
        try {
            $this->autoValidate('index', $params);
        } catch (ValidateException $e) {
            return $this->returnJsonData(100001, [], $e->getMessage() ?? '');
        }

        $where = [
            ['is_del', '=', 0],
            ['order_id', '=', intval($params['order_id'])]
        ];

        $res = $this->service->getList($where);
        return $this->returnJsonData($res['code'], $res['data'] ?? [], $res['message'] ?? '');
    }

    /**
     * @describe 创建部分发货申请
     * @return \think\Response
     */
    public function create()
    {
        $params = $this->request->param();
        try {
            $this->autoValidate('create', $params);
        } catch (ValidateException $e) {
            return $this->returnJsonData(100001, [], $e->getMessage() ?? '');
        }

        $params['creator'] = $this->operator_id;
        $result = $this->service->create($params);
        return $this->returnJsonData($result['code'], $result['data'] ?? [], $result['message'] ?? '');
    }
    /**
     * @describe 创建部分发货申请
     * @return \think\Response
     */
    public function cancel()
    {
        $params = $this->request->param();
        try {
            $this->autoValidate('cancel', $params);
        } catch (ValidateException $e) {
            return $this->returnJsonData(100001, [], $e->getMessage() ?? '');
        }

        $params['creator'] = $this->operator_id;
        $result = $this->service->cancel($params);
        return $this->returnJsonData($result['code'], $result['data'] ?? [], $result['message'] ?? '');
    }
    /**
     * @describe 审核部分发货申请
     * @return \think\Response
     */
    public function approve()
    {
        $params = $this->request->param();
        try {
            $this->autoValidate('approve', $params);
        } catch (ValidateException $e) {
            return $this->returnJsonData(100001, [], $e->getMessage() ?? '');
        }

        $params['updater'] = $this->operator_id;
        $result = $this->service->approve($params);
        return $this->returnJsonData($result['code'], $result['data'] ?? [], $result['message'] ?? '');
    }
}
